﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Controllers
{
    public class BirthdayController : BaseController
    {
        //分页查询
        public IQueryable<BirthdayBlessDTO> Find(int empId,int start, int limit, out int cnt)
        {
            IQueryable<BirthdayBlessDTO> list = from r in ctx.BirdayBlesses where r.EmpId == empId orderby r.BlessDate descending
                                                select new BirthdayBlessDTO{ FromName = r.Employee1.Name, BlessContent = r.BlessContent, BlessDate = r.BlessDate };
            cnt = list.Count();
            IQueryable<BirthdayBlessDTO> listLimit = list.Skip(start).Take(limit);
            return listLimit;
        }

        public ActionResult Add(string json)
        {
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            var rdto = new ResultMessages();
            rdto.Message = "";
            JavaScriptSerializer jss = new JavaScriptSerializer();
            var obj = jss.Deserialize<BirdayBless>(json);
            obj.FromId =ebi.Id;
            obj.BlessDate = DateTime.Now;
            try
            {
                ctx.BirdayBlesses.InsertOnSubmit(obj);
                ctx.SubmitChanges();
                rdto.Message = "新增成功";
                rdto.Result = true;
            }
            catch (Exception ex)
            {
                rdto.Message = "新增失败";
                rdto.Result = false;
            }
            return Json(rdto);
        }
    }
}